//
//  YWUnLikeView.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/28.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWUnLikeView.h"
#import "Constants.h"
#import <UIView+SDAutoLayout.h>
#import <UIImageView+WebCache.h>

@interface YWUnLikeView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation YWUnLikeView
{
    NSMutableArray *frameArray;
}

-(instancetype)initWithUnLikeArray:(NSArray *)array;
{
    if (self = [super init])
    {
        
        [self setUpViews:array];
    }
    return self;
}

-(void)setUpViews:(NSArray *)array
{
    _unLikeArray = [array copy];
    frameArray = [NSMutableArray array];
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT)];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.3;
    bgView.userInteractionEnabled = YES;
    [self addSubview:bgView];
    self.frame = CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT);
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cleanTap)];
    [bgView addGestureRecognizer:tap];
    [self addSubview:self.collectionView];
    
    int i=0;
    for (NSString *name in array)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:name forState:UIControlStateNormal];
        [btn sizeToFit];
        [frameArray addObject:[NSValue valueWithCGSize:btn.size]];
        i++;
    }
    [self.collectionView reloadData];
    [self.collectionView numberOfSections];
}
-(UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView                    = [[UICollectionView alloc]initWithFrame:CGRectMake((MAINWIDTH-250)/2.0, (MAINHEIGHT-150)/2.0, 250, 150) collectionViewLayout:layout];
        layout.sectionInset = UIEdgeInsetsMake(10,10, 10, 10);
        _collectionView.delegate           = self;
        _collectionView.dataSource         = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.layer.cornerRadius = 5;
    cell.layer.borderWidth  = 1;
    cell.layer.borderColor  = [UIColor grayColor].CGColor;
    
    if (indexPath.row == _unLikeArray.count-1)
    {
        CGRect rect = collectionView.frame;
        UICollectionViewCell *lastCell = cell;
        collectionView.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, lastCell.frame.size.height+lastCell.frame.origin.y+10);
    }
    UILabel *textLabel = [UILabel new];
    textLabel.textAlignment = 1;
    [cell addSubview:textLabel];
    
    textLabel.sd_layout
    .spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    textLabel.text = [[_unLikeArray[indexPath.row] componentsSeparatedByString:@"/"] firstObject];
    textLabel.font = [UIFont systemFontOfSize:14];
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _unLikeArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSValue *value = frameArray[indexPath.row];
    return [value CGSizeValue];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectBlock)
    {
        _selectBlock([[_unLikeArray[indexPath.row]componentsSeparatedByString:@"/"] firstObject]);
    }
    [self cleanTap];
    
}


-(void)cleanTap
{
    [self removeFromSuperview];
}
@end
